from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from .models import *
from shop.models import QUser
from .form import *
from shop.views import is_login
import hashlib


# 首页路由
@is_login
def index(request):
    return render(request, 'store/index.html', locals())


# 商品添加
@is_login
def goods(request):
    type_list = GoodsType.objects.all()
    Goods_list = Goods.objects.all()
    if request.method == 'POST':
        g_name = request.POST.get('goods_name')
        g_type = request.POST.get('goods_type')
        g_price = request.POST.get('g_price')
        g_safe_date = request.POST.get('g_safe_date')
        g_publish_time = request.POST.get('g_public_date')
        g_safe_unit = request.POST.get('g_safe_unit')
        g_description = request.POST.get('g_description')
        g_number = request.POST.get('goods_number')
        g_unit = request.POST.get('goods_unit')
        g_picture = request.FILES.get('g_picture')
        print(g_picture)
        g = Goods()
        g.g_name = g_name
        g.g_type = GoodsType.objects.filter(type=g_type).first()
        g.g_price = g_price
        g.save_time = g_safe_date
        g.save_unit = g_safe_unit
        g.publish_time = g_publish_time
        g.g_number = g_number
        g.g_unit = g_unit
        g.g_picture = g_picture
        g.g_description = g_description
        ID = request.COOKIES.get('user_id')
        g.g_store = Store.objects.filter(id=ID).first()
        g.save()
    return render(request, 'store/goods.html', locals())


# 类型展示
def type_show(request):
    type_list = GoodsType.objects.all()
    return render(request, 'store/type_manage.html', locals())


# 类型编辑
def type_manage(request):
    if request.method == 'POST':
        type_id = request.POST.get('t_id')
        type = request.POST.get('t_type')
        picture = request.FILES.get('t_picture')
        logo = request.FILES.get('t_logo')
        if type_id:
            Type = GoodsType.objects.filter(id=int(type_id)).first()
        else:
            Type = GoodsType()
        Type.type = type
        Type.t_picture = picture
        Type.t_logo = logo
        Type.save()
        type_list = GoodsType.objects.all()
        return render(request, 'store/type_manage.html', locals())


# 删除类型
def delete_type(request, t):
    Type = GoodsType.objects.filter(id=int(t))
    Type.delete()
    type_list = GoodsType.objects.all()
    return render(request, 'store/type_manage.html', locals())


# 商品统计
@is_login
def statistics_goods(request):
    return render(request, 'store/statistics_goods.html', locals())


# 订单确认
@is_login
def orders(request):
    return render(request, 'store/orders.html', locals())


# 订单统计
@is_login
def statistics_orders(request):
    return render(request, 'store/statistics_orders.html', locals())


# 店铺管理
@is_login
def store(request):
    user = QUser.objects.filter(id = int(request.COOKIES.get('user_id'))).first()
    if request.method == 'POST':
        s_name = request.POST.get('s_name')
        s_address = request.POST.get('s_address')
        s_description = request.POST.get('s_description')
        s_picture = request.FILES.get('s_picture')
        s = user.store
        s.s_name = s_name
        s.s_address = s_address
        s.s_description = s_description
        if s_picture:
            s.s_picture = s_picture
        s.save()
    return render(request, 'store/store.html', locals())
